var currentNode = null;
var thisEvent = null;
function handler(event) {
    if (event.button == 2) {// 右键
        // 当前位置弹出菜单（div）
        if (currentNode.elementType == "node") {
            thisEvent = event;
            $("#contextmenu").css({
                top: event.pageY,
                left: event.pageX
            }).show();
        } else {
            thisEvent = event;
            $("#contextmenu2").css({
                top: event.pageY,
                left: event.pageX
            }).show();
        }
    }
}

function initARightButton(n) {
    n.addEventListener('mouseup', function (event) {
        currentNode = this;
        handler(event);
    });
}

function initDBClick() {
    var textfield = $("#jtopo_textfield");
    scene.dbclick(function (event) {
        if (event.target == null) return;
        var e = event.target;
        textfield.css({
            top: event.pageY,
            left: event.pageX - e.width / 2
        }).attr('value', e.text).show().focus().select();
        e.text = "";
        textfield[0].JTopoNode = e;
    });
    $("#jtopo_textfield").blur(function () {
        textfield[0].JTopoNode.text = textfield.hide().val();
    });
}

function initRightButton() {
    var beginNode;
    var tempNodeA = new JTopo.Node('tempA');
    tempNodeA.setSize(1, 1);
    var tempNodeZ = new JTopo.Node('tempZ');
    tempNodeZ.setSize(1, 1);
    var tempLink = new JTopo.Link(tempNodeA, tempNodeZ);
    scene.mouseup(function (e) {
        if (e.button == 2) {
            scene.remove(tempLink);
            return;
        }
        if (e.target != null && e.target instanceof JTopo.Node) {
            if (beginNode !== e.target) {
                var endNode = e.target;
                var l = new JTopo.Link(beginNode, endNode);
                initARightButton(l);
                scene.add(l);
                beginNode = null;
                scene.remove(tempLink);
            }
        } else {
            scene.remove(tempLink);
        }
    });
    scene.mousedown(function (e) {
        if (e.target == null || e.target === beginNode || e.target === tempLink) {
            scene.remove(tempLink);
        }
    });
    scene.mousemove(function (e) {
        tempNodeZ.setLocation(e.x, e.y);
    });
    var k = scene.getDisplayedElements();
    var kLength = k.length;
    for (var i = kLength - 1; i >= 0; i--) {
        var node = k[i];
        if (node != undefined) {
            node.addEventListener('mouseup', function (event) {
                currentNode = this;
                handler(event);
            });
        }
    }
    stage.click(function (event) {
        if (event.button == 0) {// 右键
            // 关闭弹出菜单（div）
            $("#contextmenu").hide();
            $("#contextmenu2").hide();
            thisEvent = null;
        }
    });
    $("#contextmenu a").click(function () {
        var text = $(this).text();
        if (text == '开始连线') {
            //currentNode.fillColor=
            //scene.remove(currentNode);
            if (thisEvent.target != null && thisEvent.target instanceof JTopo.Node) {
                if (beginNode == null) {
                    beginNode = thisEvent.target;
                    scene.add(tempLink);
                    tempNodeA.setLocation(thisEvent.x, thisEvent.y);
                    tempNodeZ.setLocation(thisEvent.x, thisEvent.y);
                } else if (beginNode !== thisEvent.target) {
                    var endNode = thisEvent.target;
                    var l = new JTopo.Link(beginNode, endNode);
                    l.lineWidth = 1;
                    initARightButton(l);
                    scene.add(l);
                    beginNode = null;
                    scene.remove(tempLink);
                } else {
                    beginNode = null;
                }
            } else {
                scene.remove(tempLink);
            }
            currentNode = null;
            thisEvent = null;
            $("#contextmenu").hide();
        }
        if(text=='绑定数据'){
        	$('#bundlingData').dialog('open');
        	$("#contextmenu").hide();
        }
        if(text=='绑定事件'){
        	alert(currentNode);
        	$('#bundlingEvent').dialog('open');
        	$("#contextmenu").hide();
        }
        if (text == '设置成告警') {
            //currentNode.fillColor=
            //scene.remove(currentNode);
            currentNode.alarm = "严重告警";
            currentNode = null;
            $("#contextmenu").hide();
        }
        if (text == '闪烁告警') {
            //currentNode.fillColor=
            //scene.remove(currentNode);
            currentNode = null;
        }
        if (text == '清除告警') {
            currentNode.alarm = undefined;
            //currentNode.fillColor=
            //scene.remove(currentNode);
            currentNode = null;
            $("#contextmenu").hide();
        }
        if (text == '设置文字颜色') {
            currentNode = null;
        }
        if (text == '设置文字大小') {
            //scene.remove(currentNode);
            currentNode = null;
        }
        if (text == '删除该节点') {
            scene.remove(currentNode);
            currentNode = null;
            $("#contextmenu").hide();
        }
        if (text == '撤销上一次操作') {
            currentNode.restore();
            $("#contextmenu").hide();
        }
        if (text == '更改颜色') {
            currentNode.fillColor = JTopo.util.randomColor();
        } else if (text == '顺时针旋转') {
            currentNode.rotate += 0.5;
        } else if (text == '逆时针旋转') {
            currentNode.rotate -= 0.5;
        } else if (text == '放大') {
            currentNode.scaleX += 0.2;
            currentNode.scaleY += 0.2;
        } else if (text == '缩小') {
            currentNode.scaleX -= 0.2;
            currentNode.scaleY -= 0.2;
        }
        $("#contextmenu").hide();
    });
    $("#contextmenu2 a").click(function () {
        var text = $(this).text();
        if (text == '删除连线') {
            scene.remove(currentNode);
            currentNode = null;
            $("#contextmenu2").hide();
        }
        if (text == '设置颜色') {
        	$('#colorcaiter').dialog('open');
            //currentNode.strokeColor = '182,41,43';
            //currentNode = null;
            $("#contextmenu2").hide();
        }
        if (text == '设置线条宽度') {
        	$('#lineMain').dialog('open');
            //currentNode.strokeColor = '182,41,43';
            //currentNode = null;
            $("#contextmenu2").hide();
        }
        if (text == '设置线条类型') {
        	$('#lineClassMain').dialog('open');
            //currentNode.strokeColor = '182,41,43';
            //currentNode = null;
            $("#contextmenu2").hide();
        }
    });
}

function newLink(nodeA, nodeZ, text, dashedPattern){
    var link = new JTopo.Link(nodeA, nodeZ, text);        
    link.lineWidth = 3; // 线宽
    link.dashedPattern = dashedPattern; // 虚线
    link.bundleOffset = 60; // 折线拐角处的长度
    link.bundleGap = 20; // 线条之间的间隔
    link.textOffsetY = 3; // 文本偏移量（向下3个像素）
    link.strokeColor = '0,200,255';
    scene.add(link);
    return link;
}


